<template>
    <div class="index_box">
        <mapContent ref="mapContentDom"></mapContent>
        <Login ref="loginDom" v-show="loginShow" @changeName="changeName"></Login>
    </div>
</template>

<script>
import mapContent from './mapContent';
import Login from './userLogin/login';
import { checkEmailToken } from '@/service/user';
import dataCenterBus from 'U/dataCenterBus';
export default {
    name: 'indexMean',
    components: {
        mapContent,
        Login,
    },
    data() {
        return {
            loginShow: true,
        };
    },
    mounted() {
        this.loginShow = false;
        // 已经登录，进入系统
        if (
            localStorage.getItem('userAuthorization') &&
            localStorage.getItem('userAuthorization') !== 'undefined'
        ) {
            this.loginShow = false;
        }
        dataCenterBus.$on('isLogin', val => {
            this.loginShow = val;
        });
        // 带有token，激活用户
        if (window.location.href.indexOf('token') > -1) {
            let token = window.location.href.substring(window.location.href.indexOf('token=') + 6);
            this.activationAndGetUser(token);
            dataCenterBus.$emit('isLogin', false);
        }
    },
    methods: {
        // 改变用户名
        changeName(name) {
            this.$refs['mapContentDom'].userName = name;
        },
        changeForm(name) {
            this.$refs['loginDom'].changeForm(name);
        },
        async activationAndGetUser(token) {
            let resData = await checkEmailToken(token);
            if (resData.status === 200) {
                if (resData.data.code !== '200') {
                    this.$message.warning(resData.data.message);
                    return;
                }
                this.userInfoAll = {
                    name: resData.data.user.name,
                    token: token,
                };
                localStorage.setItem('userAuthorization', token);
                localStorage.setItem('userInf', JSON.stringify(this.userInfoAll));
                this.changeName(resData.data.user.name);
                this.loginShow = false;
                this.$router.replace({
                    path: '/',
                    query: {},
                });
            } else {
                this.$message.error('邮箱激活出了点问题，请联系我们');
            }
        },
    },
};
</script>
<style lang="scss">
@import './theme/css/index.scss';
</style>
